package com.hy.appium1;

import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;

public class AppiumDemo02Element02 {

//    AppiumDriver driver;


    AndroidDriver driver;

    @BeforeMethod
    public void SetUp() throws MalformedURLException {
        //1.需要创建配置对象   desiredcapabilities
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        //将需要启动的软件的一些配置添加进来
        //1.添加需要测试 的主机的路径
        desiredCapabilities.setCapability("deviceName", "127.0.0.1:62001");
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("appPackage", "com.handsgo.jiakao.android");
        //默认启动不初始化软件
//        desiredCapabilities.setCapability("noReset", "true");
        desiredCapabilities.setCapability("appActivity", "com.handsgo.jiakao.android.splash.Login");
        //创建驱动
        driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);
//        driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);
//        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void test() throws InterruptedException {
//元素点击操作
        Thread.sleep(5000);

        WebElement byId = driver.findElementById("com.handsgo.jiakao.android:id/btn_agree");
        byId.click();
        //不再提醒   com.handsgo.jiakao.android:id/permission_box
        Thread.sleep(20000);

        driver.findElementById("com.handsgo.jiakao.android:id/permission_box").click();
        Thread.sleep(5000);

        //知道了       com.handsgo.jiakao.android:id/permission_btn
        driver.findElementById("com.handsgo.jiakao.android:id/permission_btn").click();
        Thread.sleep(5000);


        //  定位中   com.handsgo.jiakao.android:id/cityTv

        //显示等待   需要实例化webDriverWait对象
        WebDriverWait wait = new WebDriverWait(driver, 10);

        WebElement element = wait.until(new ExpectedCondition<WebElement>() {
            public WebElement apply(WebDriver webDriver) {
                return driver.findElementById("com.handsgo.jiakao.android:id/cityTv");
            }

        });

        element.click();

        //文本框的id    com.handsgo.jiakao.android:id/edt_search_q
        WebElement webElement = driver.findElementById("com.handsgo.jiakao.android:id/edt_search_q");
//        webElement.sendKeys("上");
        //搜索到的具体的位置  com.handsgo.jiakao.android:id/item_title
//        List<WebElement> list = driver.findElementsById("com.handsgo.jiakao.android:id/item_title");
//        for (WebElement element : list) {
//            System.out.println(element.getText());
//        }
//        list.get(0).click();
        //点击  城市

//        driver.findElement(By.name("上海")).click();

        // 原生的自动化引擎
//        driver.findElementByAndroidUIAutomator("new UiSelector().text(\"上海\")").click();

        driver.findElementByXPath("//android.widget.TextView[@text='上饶']").click();


        //  男     com.handsgo.jiakao.android:id/maleTv
        driver.findElementById("com.handsgo.jiakao.android:id/maleTv").click();
        // 已报名驾校     com.handsgo.jiakao.android:id/unRegisterTv
        driver.findElementById("com.handsgo.jiakao.android:id/registeredTv").click();

//        //下一步   com.handsgo.jiakao.android:id/okBtn
//
//        driver.findElementById("com.handsgo.jiakao.android:id/okBtn").click();
//
//        //点击驾校名     com.handsgo.jiakao.android:id/hot1
//
//        driver.findElementById("com.handsgo.jiakao.android:id/hot1").click();
//
//        //选价钱    com.handsgo.jiakao.android:id/stage_2000_4000
//
//        driver.findElementById("com.handsgo.jiakao.android:id/stage_2000_4000").click();
//
//        //点击弹窗的关闭
//
//        driver.findElementById("com.handsgo.jiakao.android:id/closeIv").click();
//
//        String ex = ".main.activity.MainActivity";
//
//        String currentActivity = driver.currentActivity();
//
//        System.out.println(currentActivity);
//
//        Assert.assertEquals(currentActivity,ex);


    }

    @AfterMethod
    public void TestDown() {
//        driver.quit();
    }
}
